import sys
from pathlib import Path

base_path = str(Path(__file__).resolve().parent.parent)
sys.path.append(base_path)
from langchain.chains.openai_functions import create_structured_output_chain
from langchain_core.prompts import ChatPromptTemplate

from create_llm import create_llm


def get_current_weather(location: str, unit: str = "celsius"):
    """
    获取指定地点的天气信息
    :param location: 城市/地点的名称
    :param unit: 温度单位：celsius、fahrenheit
    :return: dict，包含天气信息的字段
    """
    weather_info = {
        "location": location,
        "temperature": 22,
        "unit": unit,
        "forcast": ["sunny", "windy"],
    }
    return weather_info


response_schema = {
    "function_call": "get_current_weather",
    "properties": {
        "location": {"type": "string", "description": "城市/地点的名称"},
        "unit": {
            "type": "string",
            "enum": ["celsius", "fahrenheit"],
            "description": " 温度单位：celsius、fahrenheit",
        },
    },
    "required": ["location"],
}

prompt = ChatPromptTemplate.from_messages(
    [
        ('system', "你是一个智能助手，你需要根据问题回答相关的天气信息"),
        ('human', "{input}"),
    ]
)

chain = create_structured_output_chain(
    prompt=prompt,
    output_schema=response_schema,
    llm=create_llm(code='deepseek'),
    # verbose=True,
)

output = chain.invoke({"input": '用华氏度说明南京的天气怎么样'})

print(type(output), output)

weather_data = get_current_weather(**output.get("function"))

print(weather_data)
